﻿using System;
using System.Web;
using System.Collections;
using System.ComponentModel;
using System.Web.SessionState;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web.Security;
using LTP.Accounts.Bus;
using System.Configuration;
namespace jnagr.Web 
{
	/// <summary>
	/// Global 的摘要说明。
	/// </summary>
	public class Global : System.Web.HttpApplication
	{
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.IContainer components = null;
        static public System.Timers.Timer myTimer = new System.Timers.Timer();
        public static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>                                                                             
        /// 定时器初始化                                                                          
        /// </summary>                                                                            
        private void sysTimerInit()
        {
            myTimer.Interval = 1000 * 60 * 60 * 1;//1个小时
            myTimer.Elapsed += new System.Timers.ElapsedEventHandler(sysTimer_Elapsed);
            myTimer.AutoReset = true;
            myTimer.Enabled = true;
            //开始启动
            myTimer.Start();
        }

        /// <summary>                                                                             
        /// 定时任务事件委托                                                                      
        /// </summary>                                                                            
        /// <param name="sender"></param>                                                         
        /// <param name="e"></param>                                                              
        private void sysTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //执行开始或结束日期
            string tbcbdtime = ConfigurationManager.AppSettings["tbcbdtime"];
            string tbybqtime = ConfigurationManager.AppSettings["tbybqtime"];
            string tbxqhtime = ConfigurationManager.AppSettings["tbxqhtime"];
            string cbdtime = ConfigurationManager.AppSettings["cbdtime"];
            string cbdtimeadd = ConfigurationManager.AppSettings["cbdtimeadd"];
            string ybqtime = ConfigurationManager.AppSettings["ybqtime"];
            BLL.devinfo bll=new BLL.devinfo();
            BLL.autosetinfo autobll = new BLL.autosetinfo();
            BLL.weatherlink wlbll = new BLL.weatherlink();
            DateTime dtnow = DateTime.Now;
            if (tbcbdtime!=null && tbcbdtime != "-1")
            {
                int cbd=0;
                int.TryParse(tbcbdtime,out cbd);
                if(cbd!=0)
                    if (dtnow.Hour == cbd)
                    {
                        DataSet ds = autobll.GetList("DevType='CBJ'");
                        DateTime cdt;
                        string devid;
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            DateTime.TryParse(ds.Tables[0].Rows[i]["mdatetime"].ToString(), out cdt);
                            devid=ds.Tables[0].Rows[i]["devid"].ToString();
                            bll.Addcbd(devid, cdt, cbdtime,cbdtimeadd);
                        }
                    }               
            }
            if (tbybqtime != null && tbybqtime != "-1")
            {
                int ybq = 0;
                int.TryParse(tbybqtime, out ybq);
                if (ybq != 0)
                    if (dtnow.Hour == ybq)
                    {
                        DataSet ds = autobll.GetList("DevType='YBQ'");
                        DateTime cdt;
                        string devid;
                        int hs = 1;
                        int.TryParse(ybqtime, out hs);
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            DateTime.TryParse(ds.Tables[0].Rows[i]["mdatetime"].ToString(), out cdt);
                            devid = ds.Tables[0].Rows[i]["devid"].ToString();
                            bll.Addybq(devid, cdt, hs);
                        }
                    }
            }
            if (tbxqhtime != null && tbxqhtime != "-1")
            {
                int xqh = 0;
                int.TryParse(tbxqhtime, out xqh);
                if (xqh != 0)
                    if (dtnow.Hour == xqh)
                    {
                        DataSet ds = wlbll.GetList("");
                        DateTime cdt;
                        string devidmaster;
                        string devidslave;
                        int hs = 1;
                        int.TryParse(ybqtime, out hs);
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            DateTime.TryParse(ds.Tables[0].Rows[i]["mdatetimeslave"].ToString(), out cdt);
                            devidmaster = ds.Tables[0].Rows[i]["devidmaster"].ToString();
                            devidslave = ds.Tables[0].Rows[i]["devidslave"].ToString();
                            bll.Addxqh(devidmaster, devidslave,cdt);                            
                        }
                    }
            }    

        }                          
		public Global()
		{
			InitializeComponent();
		}	
		
		protected void Application_Start(Object sender, EventArgs e)
		{
            this.sysTimerInit();
            #region 默认蓝
            Application["titlename"] = ConfigurationManager.AppSettings["titlename"].ToString();

            Application["1xtop1_bgimage"]="images/top-1.gif"; //顶框背景图片
			Application["1xtop2_bgimage"]="images/top-2.gif"; //顶框背景图片
			Application["1xtop3_bgimage"]="images/top-3.gif"; //顶框背景图片
			Application["1xtop4_bgimage"]="images/top-4.gif"; //顶框背景图片
			Application["1xtop5_bgimage"]="images/top-5.gif"; //顶框背景图片
			Application["1xtopbj_bgimage"]="images/top-bj.gif"; //顶框背景图片

			Application["1xtopbar_bgimage"]="images/topbar_01.jpg"; //顶框工具条背景图片
			Application["1xfirstpage_bgimage"]="images/dbsx_01.gif"; //首页背景图片
			Application["1xforumcolor"]="#f0f4fb";
			Application["1xleft_width"]="204"; //左框架宽度
			
			Application["1xtree_bgcolor"]="#e3eeff"; //左框架树背景色
			Application["1xleft1_bgimage"]="images/left-1.gif"; 
			Application["1xleft2_bgimage"]="images/left-2.gif"; 
			Application["1xleft3_bgimage"]="images/left-3.gif"; 
			Application["1xleftbj_bgimage"]="images/left-bj.gif"; 

			Application["1xspliter_color"]="#6B7DDE"; //分隔块色

			Application["1xdesktop_bj"]="";//images/right-bj.gif
			Application["1xdesktop_bgimage"]="images/desktop_01.gif";//right.gif

			Application["1xtable_bgcolor"]="#F5F9FF"; //最外层表格背景
			Application["1xtable_bordercolorlight"]="#4F7FC9"; //中层表格亮边框
			Application["1xtable_bordercolordark"]="#D3D8E0"; //中层表格暗边框
			Application["1xtable_titlebgcolor"]="#E3EFFF"; //中层表格标题栏

			Application["1xform_requestcolor"]="#E78A29"; //表单中必填字段*颜色
			Application["1xfirstpage_topimage"]="images/top_01.gif";
			Application["1xfirstpage_bottomimage"]="images/bottom_01.gif";
			Application["1xfirstpage_middleimage"]="images/bg_01.gif";
			#endregion 		

		}
 
		protected void Session_Start(Object sender, EventArgs e)
		{
			Session["Style"]=1;
		}
		protected void Application_BeginRequest(Object sender, EventArgs e)
		{
		}
		protected void Application_EndRequest(Object sender, EventArgs e)
		{
		}
		protected void Application_AuthenticateRequest(Object sender, EventArgs e)
		{
		}
        protected void Application_Error(Object sender, EventArgs e)
        {
            Exception lastError = Server.GetLastError();

            if (lastError != null)
            {
                if (lastError is HttpException)
                {
                    Server.ClearError(); // 清除错误状态
                    
                }
                log.Error("Log4错误", lastError);

            }
        }
		protected void Session_End(Object sender, EventArgs e)
		{		
			
		}
		protected void Application_End(Object sender, EventArgs e)
		{
		}
			
		#region Web 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.components = new System.ComponentModel.Container();
		}
		#endregion
	}
}

